package data

import (
	"fmt"
	"yunj/pkg/global"
	"yunj/pkg/yunj/app/model"
	"yunj/pkg/yunj/core/db"
)

// 初始化成员数据库
func InitMemberDbData() (err error) {
	memberDb := db.Model(&model.AdminMember{})
	tableName := memberDb.GetTableName()
	// 创建数据表
	tableSql := "CREATE TABLE IF NOT EXISTS `" + tableName + "` (" +
		"`id` int NOT NULL AUTO_INCREMENT," +
		"`pid` int unsigned NOT NULL DEFAULT '0' COMMENT '创建者ID'," +
		"`username` varchar(64) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci NOT NULL COMMENT '用户名'," +
		"`password_salt` varchar(32) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci NOT NULL COMMENT '密码salt'," +
		"`password_hash` varchar(32) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci NOT NULL COMMENT '密码hash'," +
		"`name` varchar(32) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci NOT NULL DEFAULT '' COMMENT '姓名'," +
		"`last_login_ip` varchar(64) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci NOT NULL DEFAULT '' COMMENT '最后一次登录IP'," +
		"`last_login_at` datetime DEFAULT NULL COMMENT '最后一次登录时间'," +
		"`created_at` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP," +
		"`updated_at` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP," +
		"`state` tinyint unsigned NOT NULL DEFAULT '11' COMMENT '11 正常 | 22 回收站 | 33 已删除'," +
		"PRIMARY KEY (`id`) USING BTREE," +
		"UNIQUE KEY `uk_username` (`username`) USING BTREE," +
		"KEY `idx_name` (`name`) USING BTREE" +
		") ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci ROW_FORMAT=DYNAMIC COMMENT='成员表';"
	err = global.MySQL.Exec(tableSql).Error
	if err != nil {
		err = fmt.Errorf("创建数据表%s失败！%v", tableName, err)
		return
	}
	// 判断是否存在数据，不存在则加入系统数据
	var count int64
	err = global.MySQL.Model(&model.AdminMember{}).Count(&count).Error
	if err != nil {
		err = fmt.Errorf("数据表%s数据是否存在查询异常！%v", tableName, err)
		return
	}
	if count <= 0 {
		err = global.MySQL.Exec("INSERT INTO `yj_admin_member` VALUES (1, 0, 'admin', '5LSHffEvbJ', 'fc986db7b729ad753ba139037eececfe', '管理员', '127.0.0.1', '2025-07-01 09:21:03', '2022-01-01 00:00:00', '2025-07-01 09:21:03', 11);").Error
		if err != nil {
			err = fmt.Errorf("数据表%s系统insert数据异常！%v", tableName, err)
			return
		}
	}
	return
}

// 初始化成员部门数据库
func InitMemberDepartmentDbData() (err error) {
	memberDepartmentDb := db.Model(&model.AdminMemberDepartment{})
	tableName := memberDepartmentDb.GetTableName()
	// 创建数据表
	tableSql := "CREATE TABLE IF NOT EXISTS `" + tableName + "` (" +
		"`member_id` int NOT NULL," +
		"`department_id` int NOT NULL," +
		"PRIMARY KEY (`member_id`,`department_id`) USING BTREE" +
		") ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci ROW_FORMAT=DYNAMIC COMMENT='成员部门关系表';"
	err = global.MySQL.Exec(tableSql).Error
	if err != nil {
		err = fmt.Errorf("创建数据表%s失败！%v", tableName, err)
	}
	return
}

// 初始化成员部门岗位数据库
func InitMemberDepartmentPostionDbData() (err error) {
	memberDepartmentPostionDb := db.Model(&model.AdminMemberDepartmentPostion{})
	tableName := memberDepartmentPostionDb.GetTableName()
	// 创建数据表
	tableSql := "CREATE TABLE IF NOT EXISTS `" + tableName + "` (" +
		"`member_id` int NOT NULL," +
		"`department_postion_id` int NOT NULL," +
		"PRIMARY KEY (`member_id`,`department_postion_id`) USING BTREE" +
		") ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci ROW_FORMAT=DYNAMIC COMMENT='成员岗位关系表';"
	err = global.MySQL.Exec(tableSql).Error
	if err != nil {
		err = fmt.Errorf("创建数据表%s失败！%v", tableName, err)
	}
	return
}

// 初始化成员角色数据库
func InitMemberRoleDbData() (err error) {
	memberRoleDb := db.Model(&model.AdminMemberRole{})
	tableName := memberRoleDb.GetTableName()
	// 创建数据表
	tableSql := "CREATE TABLE IF NOT EXISTS `" + tableName + "` (" +
		"`member_id` int NOT NULL," +
		"`role_id` int NOT NULL," +
		"PRIMARY KEY (`member_id`,`role_id`) USING BTREE" +
		") ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci ROW_FORMAT=DYNAMIC COMMENT='成员角色关系表';"
	err = global.MySQL.Exec(tableSql).Error
	if err != nil {
		err = fmt.Errorf("创建数据表%s失败！%v", tableName, err)
		return
	}
	// 判断是否存在数据，不存在则加入系统数据
	var count int64
	err = global.MySQL.Model(&model.AdminMemberRole{}).Count(&count).Error
	if err != nil {
		err = fmt.Errorf("数据表%s数据是否存在查询异常！%v", tableName, err)
		return
	}
	if count <= 0 {
		err = global.MySQL.Exec("INSERT INTO `yj_admin_member_role` VALUES (1, 1);").Error
		if err != nil {
			err = fmt.Errorf("数据表%s系统insert数据异常！%v", tableName, err)
			return
		}
	}
	return
}
